package cn.wolfcode.car.business.web.contorller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * 养修服务控制器
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController {
    //模板前缀
    private static final String prefix = "business/system/carPackageAudit/";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IStatementService statementService;

    //页面------------------------------------------------------------
    //列表

    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }


    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表

    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo) {
        return carPackageAuditService.query(qo);
    }


    //新增
    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit) {
        carPackageAuditService.save(carPackageAudit);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(CarPackageAudit carPackageAudit) {
        carPackageAuditService.update(carPackageAudit);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:carPackageAudit:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        carPackageAuditService.deleteBatch(ids);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:carPackageAudit:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage() {
        // model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return prefix + "deploy";
    }
         //查看流程图
    @RequestMapping(value = "/processImg")
    public void processImg(Long id, HttpServletResponse response) throws IOException {
        //记录id
        InputStream inputStream=carPackageAuditService.getProcessImageByAuditId(id);

        IOUtils.copy(inputStream,response.getOutputStream());

    }
    //审核撤销
    @RequiresPermissions("business:carPackageAudit:cancelApply")
    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id) {
       carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    //我的代办

    @RequestMapping("/todoPage")
    public String todoPage() {
        // model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return prefix + "todoPage";
    }


    //我的待办 分页结果集

    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        User user = ShiroUtils.getUser();  //当前用户
        qo.setAuditorId(user.getId());    //用户的id
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);    //状态过滤  只有审核中的 没有撤销的
        return carPackageAuditService.query(qo);
    }

    //审批人审批

    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        model.addAttribute("id", id);         //记录表id 共享到审核页面
        return prefix + "audit";
    }

    //审核确定后 接收参数(id)后共享 为了下一个页面能拿到审核记录表的信息
    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id, int auditStatus, String info){
        //审核操作
        carPackageAuditService.audit(id, auditStatus, info);
        return AjaxResult.success();
    }


    @RequestMapping("/donePage")
    public String donePage(){
        return prefix + "donePage";
    }

    //已办页面 分页结果集

    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery qo){

        User user = ShiroUtils.getUser();
        //备注信息里面有username
        qo.setInfo(user.getUserName());

        return carPackageAuditService.query(qo);
    }
}



